import threading
import time
 
def print_numbers():
    for i in range(5):
        print(f"线程执行：{i}")
        time.sleep(1)
 
# 创建线程
thread = threading.Thread(target=print_numbers)
 
# 启动线程
thread.start()
 
# 主线程继续执行
print("主线程正在运行...")
 
# 等待子线程结束
thread.join()
 
print("主线程结束")



import threading
import time
 
class MyThread(threading.Thread):
    def run(self):
        for i in range(5):
            print(f"线程执行：{i}")
            time.sleep(1)
 
# 创建线程对象
thread = MyThread()
 
# 启动线程
thread.start()
 
# 等待线程结束
thread.join()
 
print("主线程结束")


import threading
import time
 
def task(name):
    for i in range(3):
        print(f"线程 {name} 执行：{i}")
        time.sleep(1)
 
# 创建多个线程
threads = []
for i in range(3):
    thread = threading.Thread(target=task, args=(f"Thread-{i}",))
    threads.append(thread)
    thread.start()
 
# 等待所有线程结束
for thread in threads:
    thread.join()
 
print("所有线程执行完毕")



import threading
import time
 
def background_task():
    while True:
        print("后台线程运行中...")
        time.sleep(2)
 
# 创建守护线程
daemon_thread = threading.Thread(target=background_task, daemon=True)
daemon_thread.start()
 
# 主线程等待 5 秒后结束
time.sleep(5)
print("主线程结束")


import threading
 
# 共享变量
counter = 0
 
# 创建锁
lock = threading.Lock()
 
def increment():
    global counter
    for _ in range(100000):
        with lock:  # 加锁
            counter += 1
 
# 创建多个线程
threads = []
for _ in range(2):
    thread = threading.Thread(target=increment)
    threads.append(thread)
    thread.start()
 
# 等待所有线程结束
for thread in threads:
    thread.join()
 
print(f"计数结果：{counter}")


import threading
 
# 共享变量
counter = 0
 
# 创建锁
lock = threading.Lock()
 
def increment():
    global counter
    for _ in range(100000):
        with lock:  # 加锁
            counter += 1
 
# 创建多个线程
threads = []
for _ in range(2):
    thread = threading.Thread(target=increment)
    threads.append(thread)
    thread.start()
 
# 等待所有线程结束
for thread in threads:
    thread.join()
 
print(f"计数结果：{counter}")


import threading
import time
 
# 创建条件变量
condition = threading.Condition()
buffer = []
 
def producer():
    global buffer
    for i in range(5):
        with condition:
            while len(buffer) >= 2:
                condition.wait()  # 等待消费者消费
            buffer.append(i)
            print(f"生产者生产：{i}")
            condition.notify()  # 通知消费者
            time.sleep(1)
 
def consumer():
    global buffer
    for _ in range(5):
        with condition:
            while not buffer:
                condition.wait()  # 等待生产者生产
            item = buffer.pop(0)
            print(f"消费者消费：{item}")
            condition.notify()  # 通知生产者
            time.sleep(2)
 
# 创建线程
producer_thread = threading.Thread(target=producer)
consumer_thread = threading.Thread(target=consumer)
 
producer_thread.start()
consumer_thread.start()
 
producer_thread.join()
consumer_thread.join()


from concurrent.futures import ThreadPoolExecutor
import time
 
def task(name):
    print(f"任务 {name} 开始")
    time.sleep(2)
    print(f"任务 {name} 结束")
    return f"结果：{name}"
 
# 创建线程池
with ThreadPoolExecutor(max_workers=3) as executor:
    futures = [executor.submit(task, i) for i in range(5)]
 
    # 获取任务结果
    for future in futures:
        print(future.result())

import threading
 
# 创建线程局部变量
local_data = threading.local()
 
def process_data(value):
    local_data.value = value  # 为当前线程设置局部变量
    print(f"线程 {threading.current_thread().name} 的局部变量值：{local_data.value}")
 
# 创建多个线程
threads = []
for i in range(3):
    thread = threading.Thread(target=process_data, args=(i,))
    threads.append(thread)
    thread.start()
 
# 等待线程结束
for thread in threads:
    thread.join()